package com.opgea.crm.web.controller;


import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.opgea.crm.common.vo.ActivityVo;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.ActivityService;
import com.opgea.crm.services.LeadService;
import com.opgea.crm.web.modal.LoggedUser;

@Controller
@RequestMapping(value="searchactivities")
public class ActivitySearchController {

	Logger logger=Logger.getLogger(ActivitySearchController.class);
	
	@Autowired
	ActivityService activityService;
	@Autowired
	LeadService leadService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView show(HttpServletRequest request){
		return new ModelAndView("searchactivities");
	}
	
	@RequestMapping(method=RequestMethod.GET ,value="showActivities")
	public @ResponseBody
	Map<String, Object> showActivities(
			@RequestParam(required=false, value="leadId") String leadId){
		
		logger.debug("ActivitySearchController >> showActivities");
		logger.debug("ActivitySearchController >> showActivities");
		if(leadId == null){
			return null;
		}else{
			List<ActivityVo> activities = activityService.findAll();
			return JsonModelMap.success().data(activities);			
		}
	}	
	
	@RequestMapping(method=RequestMethod.GET ,value="showActivitiesByLead")
	public @ResponseBody
	Map<String, Object> showActivitiesByLead(
			@RequestParam(required=false, value="leadId") String leadId){
		
		logger.debug("ActivitySearchController >> showActivitiesByLead");
		if(leadId != null){
			List<ActivityVo> activities = activityService.findAllByLeadId(new Long(leadId));
			return JsonModelMap.success().data(activities);				
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(method=RequestMethod.GET ,value="showActivitiesByUser")
	public @ResponseBody
	Map<String, Object> showActivitiesByUser(@RequestParam(required=false, value="searchBy") String searchBy,
											 HttpServletRequest request){
		Long assignedToId = LoggedUser.getUser(request).getEmployee().getId();
		Date fromDate = Calendar.getInstance().getTime(); 
		Date toDate = Calendar.getInstance().getTime(); 

		
		if(searchBy != null){
			Calendar calendar = Calendar.getInstance();
			if(searchBy.equalsIgnoreCase("day")){
			}
			if(searchBy.equalsIgnoreCase("week")){
				calendar.set(fromDate.getYear(), fromDate.getMonth(), fromDate.getDate());
				calendar.set(Calendar.DAY_OF_WEEK , Calendar.MONDAY);
				int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
				int today = toDate.getDate();
				int lastDateOfWeek = (today+(7-dayOfWeek));
				System.out.println("DAY_OF_WEEK: "+dayOfWeek);
				System.out.println("MONTH: "+calendar.get(Calendar.MONTH));
				System.out.println("LAST_DATE_OF_WEEK: "+lastDateOfWeek);
				toDate.setDate(lastDateOfWeek);
			}
			if(searchBy.equalsIgnoreCase("month")){
				calendar.set(fromDate.getYear(), fromDate.getMonth(), fromDate.getDate());
				int maxDayInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
				toDate.setDate(maxDayInMonth);
			}
			
			List<ActivityVo> activities = activityService.findAllByDuration(assignedToId, fromDate, toDate);
			return JsonModelMap.success().data(activities);				
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(method=RequestMethod.GET ,value="showActivitiesDynamically")
	public @ResponseBody
	Map<String, Object> showDynamically(
			@RequestParam(required=false, value="criteria") String criteria,
			@RequestParam(required=false, value="value")String value){
		
		logger.debug("ActivitySearchController >> showActivities");
		if(criteria != null && value != null){
			List<ActivityVo> activities = activityService.findDynamically(criteria, value);
			return JsonModelMap.success().data(activities);	
		}else{
			return JsonModelMap.success();
		}
	}
}
